/* ----------------------------------------------------------------------
 * File: 		Taylor_Wood_1
 * Created by:		Taylor Wood
 * Email:		Tdwood2@asu.edu
 * Student ID:		1202553801
 *
 * Description:
 * This is the main file. Here we will open the input file
 * and create an output file based on the argv parameters.
 * This file also serves to read strings from the input file
 * and call isOperator, isDelimeter, and LEXER which are 
 * defined in lexer.cpp.
 * ----------------------------------------------------------------------*/

#include <iostream> 	// This is for testing purposes , I will remove this later.
#include <fstream>	// This is for reading and writing files
#include <string>	// Need them strings
#include <cstdlib>
#include "lexer.h"
#include "token.h"
#include "parser.h"

using namespace std;

bool bInStr = false;
char * pass;

int main(int argc, char *argv[]) {

	token * tok;
	tok = new token;

	int lineNum = 0;	// current line number

	std::ifstream fin;	// to read from files
	std::ofstream fout;	// to write to files
	std::string line = "";	// string variable to store the read line
	std::string word = "";	// string variable to store the split line

	// open the file and check to see if it exists
	fin.open(argv[1]);	// argv[1] contains the input file
	//fout.open(argv[2]);	// argv[2] contains the output file
	pass = argv[2];

	// if the file does not exist
	if (!fin) {
		std::cout << "The file \"" << argv[1] << "\" does not exist. Please provide another file.\n";
		return -1;
	}

	// File exists, begin reading from the file
	while (std::getline(fin, line)) {

		lineNum++;

		// navigate through the read line
		for (int i = 0; i < line.length(); i++) {

			// Check to see if the letter is an operator, delimiter, or space
			if ((isOperator(line[i]) || isDelimiter(line[i]) || line[i] == ' ') || line[i] == '\"' || line[i] == '\'') {

				// first check if we have a double or single quotes
				if (line[i] == '\"') {
					// if something is in the word pass to the lexer
					if (!word.empty()) {
						//fout << lexer(word) << "\t" << word << std::endl;
						tok = new token;
						tok->name = lexer(word);
						tok->word = word;
						tok->line = lineNum;
						// add it to the list
						addToList(tok);
						word = "";
						word = "";		// clear the word		
					}
					// continue building the string
					word += line[i];
					do {
						i++;
						word += line[i];

					} while (i < line.length() && (line[i] != '\"' || line[i - 1] == '\\'));
				}
				// first check if we have a double or single quotes
				if (line[i] == '\'') {
					// if something is in the word pass to the lexer
					if (!word.empty()) {
						//fout << lexer(word) << "\t" << word << std::endl;
						tok = new token;
						tok->name = lexer(word);
						tok->word = word;
						tok->line = lineNum;
						// add it to the list
						addToList(tok);
						word = "";
						word = "";		// clear the word		
					}
					// continue building the string
					word += line[i];
					do {
						i++;
						word += line[i];

					} while (i < line.length() && (line[i] != '\'' || line[i-1] == '\\'));
				}


				// Now that we know there is a character which splits the
				// string, lets check if our existing object is a word.
				if (word.length() > 0) {
					//fout << lexer(word) << "\t" << word << std::endl;
					tok = new token;
					tok->name = lexer(word);
					tok->word = word;
					tok->line = lineNum;
					// add it to the list
					addToList(tok);
					word = "";
					word = "";
				}

				// check to see if line[i] is an operator or delimiter
				if (isDelimiter(line[i]) || isOperator(line[i])) {
					word = line[i];
					if (isOperator(line[i+1])) {
						i++;
						word += line[i];
					}
					//fout << lexer(word) << "\t" << word << std::endl;
					tok = new token;
					tok->name = lexer(word);
					tok->word = word;
					tok->line = lineNum;
					// add it to the list
					addToList(tok);
					word = "";
					word = "";
				}

			}
			// If if it not true, start creating a word
			// found an issue where it is adding
			// \t in linux, need to check for that
			else if (line[i] == '\t') {}
			//do nothing
			else
				word += line[i];
			//}
	}
	// There may be the interesting case where a text file does not end with
	// a space, delimiter, or operator. Therefor, let's check to see what
	// is in the word	
	if (word.length() > 0) {
		//fout << lexer(word) << "\t" << word << std::endl;
		tok = new token;
		tok->name = lexer(word);
		tok->word = word;
		tok->line = lineNum;

		// add it to the list
		addToList(tok);
		word = "";
	}

}

isProgram(pass);

// don't want any memory leaks
delete tok;

return 0;

}
